package com.sweet.pay.utils;

import java.io.File;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by sweet on 2018/11/23.
 * ---------------------------
 */
public final class TimeUtils {

  private TimeUtils() {
  }

  /**
   * 获取当前时间戳
   *
   * @return
   */
  public static long now() {
    return System.currentTimeMillis();
  }

  /**
   * 当前时间字符串形式 yyyy-MM-dd HH:mm:ss
   *
   * @return
   */
  public static String nowLocal() {
    return nowFormat("yyyy-MM-dd HH:mm:ss");
  }

  /**
   * 根据 格式 显示当前日期
   *
   * @param pattern
   * @return
   */
  public static String nowFormat(String pattern) {
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
    return dateTimeFormatter.format(LocalDateTime.now());
  }

  /**
   * LocalDateTime 对象 转成 时间戳
   *
   * @param localDateTime
   * @return
   */
  public static long dateTimeToTimestamp(LocalDateTime localDateTime) {
    Timestamp t = Timestamp.valueOf(localDateTime);
    return t.getTime();
  }

  /**
   * LocalDate 对象 转成 时间戳
   *
   * @param localDate
   * @return
   */
  public static long dateTimeToTimestamp(LocalDate localDate) {
    Timestamp t = Timestamp.valueOf(localDate.atStartOfDay());
    return t.getTime();
  }

  /**
   * 时间戳 转成 LocalDateTime
   *
   * @param time
   * @return
   */
  public static LocalDateTime timestampToLocalDateTime(long time) {
    return LocalDateTime.ofInstant(Instant.ofEpochMilli(time),
        TimeZone.getDefault().toZoneId());
  }

  /**
   * 时间戳 转成 LocalDate
   *
   * @param time
   * @return
   */
  public static LocalDate timestampToLocalDate(long time) {
    return LocalDateTime.ofInstant(Instant.ofEpochMilli(time),
        TimeZone.getDefault().toZoneId()).toLocalDate();
  }

  /**
   * 时间戳 转成 字符串
   *
   * @param time
   * @param pattern
   * @return
   */
  public static String timestampToString(long time, String pattern) {
    return DateTimeFormatter.ofPattern(pattern).format(timestampToLocalDateTime(time));
  }

  /**
   * 时间戳 转成 yyyy-MM-dd HH:mm:ss 格式
   *
   * @param time
   * @return
   */
  public static String timestampToString(long time) {
    return timestampToString(time, "yyyy-MM-dd HH:mm:ss");
  }

  /**
   * 日期类型 转成 yyyy-MM-dd HH:mm:ss 字符串
   *
   * @param date
   * @return
   */
  public static String dateToString(Date date) {
    return timestampToString(date.getTime());
  }

  /**
   * Date 转成 字符串
   *
   * @param date
   * @param pattern
   * @return
   */
  public static String dateToString(Date date, String pattern) {
    return timestampToString(date.getTime(), pattern);
  }

  /**
   * Date 转成 LocalDateTime
   *
   * @param date
   * @return
   */
  public static LocalDateTime dateToLocalDateTime(Date date) {
    return timestampToLocalDateTime(date.getTime());
  }

  /**
   * Date 转成 LocalDate
   *
   * @param date
   * @return
   */
  public static LocalDate dateToLocalDate(Date date) {
    return timestampToLocalDateTime(date.getTime()).toLocalDate();
  }

  /**
   * 2018-11-26 10:01:19 转成日期对象
   *
   * @param date 字符串日期
   * @return
   */
  public static LocalDateTime strToLocalDateTime(String date) {
    return strToLocalDateTime(date, "yyyy-MM-dd HH:mm:ss");
  }

  /**
   * 2018-11-26 10:01:19 转成日期对象
   *
   * @param date 字符串日期
   * @return
   */
  public static LocalDateTime strToLocalDateTime(String date, String pattern) {
    return LocalDateTime.parse(date, DateTimeFormatter.ofPattern(pattern));
  }

  public static LocalDate strToLocalDate(String date) {
    return LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
  }

  public static LocalDate strToLocalDate(String date, String pattern) {
    return LocalDate.parse(date, DateTimeFormatter.ofPattern(pattern));
  }

  public static void main(String[] args) {
//
//    System.out.println("now: " + now());
//    System.out.println("nowLocal: " + nowLocal());
//    System.out.println("nowFormat: " + nowFormat("yyyy/MM/dd HH:mm:ss"));
//    System.out.println("timestampToLocalDateTime: " + timestampToLocalDateTime(System.currentTimeMillis()));
//    System.out.println("timestampToLocalDate: " + timestampToLocalDate(System.currentTimeMillis()));
//    System.out.println("timestampToString: " + timestampToString(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss"));
//    System.out.println("timestampToString: " + timestampToString(System.currentTimeMillis()));
//    System.out.println("dateToString: " + dateToString(new Date()));
//    System.out.println("dateToString: " + dateToString(new Date(), "yyyy-MM-dd HH:mm:ss"));
//    System.out.println("dateToLocalDateTime: " + dateToLocalDateTime(new Date()));
//    System.out.println("dateToLocalDate: " + dateToLocalDate(new Date()));
//    System.out.println("strToLocalDateTime: " + strToLocalDateTime("2018-10-11 13:10:01"));
//    System.out.println("strToLocalDateTime: " + strToLocalDateTime("2018/10/11 13/10/01", "yyyy/MM/dd HH/mm/ss"));
    System.out.println(TimeUtils.nowFormat("yyyy" + File.separator + "MM" + File.separator + "dd" + File.separator));
  }
}
